/*
 * Copyright (c) 2012 Robert Burrell Donkin robertburrelldonkin.name
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package walrus.text.concise;

public class WordCount {
	
	private int count = 0;
	private WordCount[] children = null;
	
	public void inc() {
		count++;
	}
	
	public int count() {
		return count;
	}
	
	public WordCount next(char character) {
		if (children == null) {
			children = new WordCount[26];
		}
		final int index = index(character);
		if (index<0) {
			return null;
		}
		if (children[index] == null) {
			children[index] = new WordCount();
		}
		return children[index];
	}
	
	public int index(char character) {
		switch (character) {
		case '\'':
			return 0;
		case 'a':
		case 'A':
			return 1;
		case 'b':
		case 'B':
			return 2;
		case 'c':
		case 'C':
			return 3;
		case 'd':
		case 'D':
			return 4;
		case 'e':
		case 'E':
			return 5;
		case 'f':
		case 'F':
			return 6;
		case 'g':
		case 'G':
			return 7;
		case 'h':
		case 'H':
			return 8;
		case 'i':
		case 'I':
			return 9;
		case 'j':
		case 'J':
			return 10;
		case 'k':
		case 'K':
			return 11;
		case 'l':
		case 'L':
			return 12;
		case 'm':
		case 'M':
			return 13;
		case 'n':
		case 'N':
			return 14;
		case 'o':
		case 'O':
			return 15;
		case 'p':
		case 'P':
			return 16;
		case 'q':
		case 'Q':
			return 17;
		case 'r':
		case 'R':
			return 18;
		case 's':
		case 'S':
			return 19;
		case 't':
		case 'T':
			return 20;
		case 'u':
		case 'U':
			return 21;
		case 'v':
		case 'V':
			return 22;
		case 'w':
		case 'W':
			return 23;
		case 'x':
		case 'X':
			return 24;
		case 'y':
		case 'Y':
			return 25;
		case 'z':
		case 'Z':
			return 26;
			default:
				return -1;
		}
	}
}
